package com.logistics.controller.tenant;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.logistics.common.Result;
import com.logistics.dto.DriverCreateRequest;
import com.logistics.entity.Driver;
import com.logistics.service.DriverService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 租户端 - 司机管理控制器
 */
@RestController
@RequestMapping("/tenant/driver")
public class DriverController {
    
    private final DriverService driverService;
    
    public DriverController(DriverService driverService) {
        this.driverService = driverService;
    }
    
    /**
     * 创建司机
     */
    @PostMapping
    @PreAuthorize("hasAuthority('u8g5p4j1') or hasAnyRole('SUPER_ADMIN','SITE_ADMIN')")
    public Result<Driver> createDriver(@Validated @RequestBody DriverCreateRequest request) {
        try {
            Driver driver = driverService.createDriver(request);
            return Result.success("司机创建成功", driver);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 司机列表（分页）
     */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('i9w7b3h2') or hasAnyRole('SUPER_ADMIN','SITE_ADMIN')")
    public Result<IPage<Driver>> getDriverList(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String driverStatus,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Long siteId) {
        try {
            IPage<Driver> page = driverService.getDriverList(current, size, driverStatus, keyword, siteId);
            return Result.success(page);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 司机详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('i9w7b3h2') or hasAnyRole('SUPER_ADMIN','SITE_ADMIN')")
    public Result<Driver> getDriverById(@PathVariable Long id) {
        try {
            Driver driver = driverService.getDriverById(id);
            if (driver == null) {
                return Result.error("司机不存在");
            }
            return Result.success(driver);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 更新司机
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('o6v9k2x7') or hasAnyRole('SUPER_ADMIN','SITE_ADMIN')")
    public Result<String> updateDriver(@PathVariable Long id,
                                       @Validated @RequestBody DriverCreateRequest request) {
        try {
            driverService.updateDriver(id, request);
            return Result.success("司机更新成功", null);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 删除司机
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('n3f8z5w4') or hasAnyRole('SUPER_ADMIN','SITE_ADMIN')")
    public Result<String> deleteDriver(@PathVariable Long id) {
        try {
            driverService.deleteDriver(id);
            return Result.success("司机删除成功", null);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 查询司机的所有订单
     */
    @GetMapping("/{id}/orders")
    @PreAuthorize("hasAuthority('n3f8z5w4') or hasAnyRole('SUPER_ADMIN','SITE_ADMIN')")
    public Result<?> getDriverOrders(@PathVariable Long id) {
        try {
            return Result.success(driverService.getDriverOrders(id));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
